//
//  AITabbarViewController.swift
//  你的AI
//
//  Created by 丸子 on 2024/6/3.
//

import UIKit
import CYLTabBarController

class AITabbarViewController: CYLTabBarController {
    let disposeBag = DisposeBag()
    override func viewDidLoad() {
        super.viewDidLoad()
        let musicVC = AINavigationController(rootViewController: MusicPageViewController())
        let homeVC = AINavigationController(rootViewController: HomeViewController())
        let VideoGenerateVC = AINavigationController(rootViewController: VideoGenerateVC())
        let plazaVC = AINavigationController(rootViewController: PlazaViewController())
//        let paintingVC = AINavigationController(rootViewController: PaintingPageViewController())
        let paintingVC = AINavigationController(rootViewController: CommunityViewController())
        let mineVC = AINavigationController(rootViewController: MineViewController())
        
        let tabItemsAttributes = [
            [CYLTabBarItemImage: "tabbar_community_nor", CYLTabBarItemSelectedImage: "tabbar_community_sel", CYLTabBarItemTitle:"社区", CYLTabBarItemImageInsets: NSValue(uiEdgeInsets: UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0))],
            [CYLTabBarItemImage: "tabbar_music_nor", CYLTabBarItemSelectedImage: "tabbar_music_sel", CYLTabBarItemTitle:"生成音乐", CYLTabBarItemImageInsets: NSValue(uiEdgeInsets: UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0))],
            [CYLTabBarItemImage: "tabbar_home_nor", CYLTabBarItemSelectedImage: "tabbar_home_sel", CYLTabBarItemTitle:"聊天", CYLTabBarItemImageInsets: NSValue(uiEdgeInsets: UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0))],
            [CYLTabBarItemImage: "tabbar_video_nor", CYLTabBarItemSelectedImage: "tabbar_video_sel", CYLTabBarItemTitle:"生成视频", CYLTabBarItemImageInsets: NSValue(uiEdgeInsets: UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0))],
            [CYLTabBarItemImage: "tabbar_mine_nor", CYLTabBarItemSelectedImage: "tabbar_mine_sel", CYLTabBarItemTitle:"我的", CYLTabBarItemImageInsets: NSValue(uiEdgeInsets: UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0))]
        ]
        self.tabBarItemsAttributes = tabItemsAttributes
        self.viewControllers = [paintingVC, musicVC, homeVC, VideoGenerateVC, mineVC]
        ChangeStyle()
        
        
        GlobalRx.requestErrSubject.observe(on: MainScheduler.instance).subscribe(onNext: { model in
            if model.code == 8888 { // 登录
                SqlManager.shared.delete(from: AccountModel.self)
                Appearance.shared.accoutModel = nil
                let vc = LoginViewController()
                vc.modalPresentationStyle = .fullScreen
                self.present(vc, animated: true)
                NDProgressHUD.hide()
            }else if model.code == 2000  {
                NDProgressHUD.hide()
                let v = HomeTopupPOPView()
                Appearance.addViewToWindow(v)
                v.chooseDone = { [weak self] in
                    guard let self = self else { return }
                    let vc = SubscribeViewController()
                    vc.modalPresentationStyle = .fullScreen
                    self.present(vc, animated: true)
                }
                v.chooseClose = { [weak self] in
                    guard let self = self else { return }
                    self.twoPOPView()
                }
            }else {
//                NDProgressHUD.hide()
                NDProgressHUD.show(message: model.msg ?? "")
            }
        }).disposed(by: disposeBag)
        
        GlobalRx.loginSubject.observe(on: MainScheduler.instance).subscribe(onNext: {[weak self] model in
            guard let self = self else { return }
            SqlManager.shared.delete(from: AccountModel.self)
            Appearance.shared.accoutModel = nil
            let vc = LoginViewController()
            vc.modalPresentationStyle = .fullScreen
            self.present(vc, animated: true)
        }).disposed(by: disposeBag)
        
        GlobalRx.userInfoSubject.observe(on: MainScheduler.instance).subscribe(onNext: { model in
            Appearance.shared.accoutModel = model
        }).disposed(by: disposeBag)
        
    }
    
    func twoPOPView(){
        let view = HomeTwoTopupPOPView()
        Appearance.addViewToWindow(view)
        
        view.chooseDone = { [weak self] in
            guard let self = self else { return }
            let vc = SubscribeViewController()
            vc.type = 1
            vc.modalPresentationStyle = .fullScreen
            self.present(vc, animated: true)
        }
    }
    
    func ChangeStyle(){
        // Adjust image insets
        let normalAttributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.font: UIFont(name: "PingFang-SC-Regular", size: 10) as Any,
            NSAttributedString.Key.foregroundColor: UIColor(hexString: "#7D7A92")
        ]
        let selectedAttributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.font: UIFont(name: "PingFang-SC-Regular", size: 10) as Any,
            NSAttributedString.Key.foregroundColor: UIColor(hexString: "#45DFFF")
        ]
        
        if #available(iOS 15, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = Theme.color.main
            // 设置未选中状态下的字体颜色
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
            // 设置选中状态下的字体颜色
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
            self.tabBar.standardAppearance = appearance
            self.tabBar.scrollEdgeAppearance = self.tabBar.standardAppearance
        } else {
            // iOS 14 及以下的设置方式
            self.tabBar.barTintColor = Theme.color.main
            // 设置未选中状态下的字体颜色
            UITabBarItem.appearance().setTitleTextAttributes(normalAttributes, for: .normal)
            // 设置选中状态下的字体颜色
            UITabBarItem.appearance().setTitleTextAttributes(selectedAttributes, for: .selected)
        }
        
    }
}
